package com.zheng.sch.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.LengthValidator;
import com.zheng.sch.common.constant.SchResult;
import com.zheng.sch.common.constant.SchResultConstant;
import com.zheng.sch.dao.model.SchClass;
import com.zheng.sch.dao.model.SchClassExample;
import com.zheng.sch.dao.model.SchStudent;
import com.zheng.sch.dao.model.SchStudentExample;
import com.zheng.sch.rpc.api.SchClassService;
import com.zheng.sch.rpc.api.SchStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生管理
 * Created by shuzheng on 2016/11/14.
 */
@Controller
@Api(value = "学生管理", description = "学生管理")
@RequestMapping("/manage/student")
public class SchStudentController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SchStudentController.class);

	@Autowired
	private SchStudentService schStudentService;

	@Autowired
	private SchClassService schClassService;

	@ApiOperation(value = "学生首页")
	@RequiresPermissions("sch:student:read")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/manage/student/index_2.jsp";
	}

	@ApiOperation(value = "学生列表")
	@RequiresPermissions("sch:student:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		SchStudentExample schStudentExample = new SchStudentExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			schStudentExample.setOrderByClause(sort + " " + order);
		}
		List<SchStudent> rows = schStudentService.selectByExampleForOffsetPage(schStudentExample, offset, limit);
		long total = schStudentService.countByExample(schStudentExample);
		Map<String, Object> result = new HashMap<>(2);
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}

	@ApiOperation(value = "新增学生")
	@RequiresPermissions("sch:student:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		SchClassExample schClassExample = new SchClassExample();
		schClassExample.setOrderByClause("ctime desc");
		List<SchClass> schClasses = schClassService.selectByExample(schClassExample);
		modelMap.put("schClasses", schClasses);
		return "/manage/student/create.jsp";
	}

	@ApiOperation(value = "新增学生")
	@RequiresPermissions("sch:student:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(SchStudent schStudent) {
		ComplexResult result = FluentValidator.checkAll()
				.on(schStudent.getName(), new LengthValidator(1, 200, "姓名"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new SchResult(SchResultConstant.INVALID_LENGTH, result.getErrors());
		}
		long time = System.currentTimeMillis();
		schStudent.setCtime(time);
		schStudent.setOrders(time);
		int count = schStudentService.insertSelective(schStudent);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "学生属性")
	@RequiresPermissions("sch:student:read")
	@RequestMapping(value = "/profile/{id}", method = RequestMethod.GET)
	public String profile(@PathVariable("id") int id, ModelMap modelMap) {
		SchStudent schStudent = schStudentService.selectByPrimaryKey(id);
		modelMap.put("student", schStudent);
		return "/manage/student/profile.jsp";
	}


	@ApiOperation(value = "修改学生")
	@RequiresPermissions("sch:student:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") int id, ModelMap modelMap) {
		SchClassExample schClassExample = new SchClassExample();
		schClassExample.setOrderByClause("ctime desc");
		List<SchClass> schClasses = schClassService.selectByExample(schClassExample);
		SchStudent schStudent = schStudentService.selectByPrimaryKey(id);
		modelMap.put("schClasses", schClasses);
		modelMap.put("student", schStudent);
		return "/manage/student/update.jsp";
	}

	@ApiOperation(value = "修改学生")
	@RequiresPermissions("sch:student:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, SchStudent schStudent) {
		ComplexResult result = FluentValidator.checkAll()
				.on(schStudent.getName(), new LengthValidator(1, 200, "姓名"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new SchResult(SchResultConstant.INVALID_LENGTH, result.getErrors());
		}
		schStudent.setStudentId(id);
		int count = schStudentService.updateByPrimaryKeySelective(schStudent);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "删除学生")
	@RequiresPermissions("sch:student:delete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = schStudentService.deleteByPrimaryKeys(ids);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}

}